package com.srcbox.file.ui

import android.os.Bundle
import android.os.Environment
import android.view.KeyEvent
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import com.srcbox.file.R
import com.srcbox.file.adapter.FilesList
import com.srcbox.file.data.AppSetting
import com.srcbox.file.util.EggUtil
import com.srcbox.file.util.GlobalMethods
import com.test.showdir.data.FileAdapterShare
import kotlinx.android.synthetic.main.select_file.*
import java.io.File
import java.io.FileFilter

class Select : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.select_file)
        GlobalMethods(this).initActivityUi()


//        val path = Environment.getExternalStorageDirectory().toString()
        FileAdapterShare.currentFilePath = AppSetting.fileStoragePath
        File(FileAdapterShare.currentFilePath).let {
            if (!it.exists()) {
                it.mkdirs()
            }
        }
        var cp = FileAdapterShare.currentFilePath
        if (cp[cp.length - 1] == '/') {
            cp = cp.substring(0, cp.length - 1)
        }
        FileAdapterShare.currentFilePath = cp
        /*if (!File(AppSetting.fileStoragePath).exists()) {
            FileAdapterShare.currentFilePath =
                Environment.getExternalStorageDirectory().absolutePath+"/"
        }*/
        /*val pathStr = "${filesDir.absolutePath}/···"
        File(pathStr).let {
            if (!it.exists()) {
                it.mkdirs()
            }
        }*/
        val fileList = File(FileAdapterShare.currentFilePath)
        val listF: Array<File?>? = fileList.listFiles(FileFilter {
            println("手势")
            it.isDirectory
        })

        /*      if (listF?.size == 0) {
                  listF = listF.let { EggUtil.concat<File>(arrayOf(File(pathStr)), it) }
              }*/

        listF?.sort()
        println(FileAdapterShare.currentFilePath)
        val filesAdapter = listF?.let { FilesList(this, it) }
        filesListV.layoutManager = LinearLayoutManager(this)
        filesListV.adapter = filesAdapter
        findViewById<TextView>(R.id.title_text).text =
            FileAdapterShare.currentFilePath
        select.setOnClickListener {
            /* val settP = filesDir.absolutePath + "/setting1.1.json"
             val jsonO = JSON.parseObject(EggIO().readFile(settP))
             jsonO["fileStoragePath"] = FileAdapterShare.currentFilePath + ""
             AppSetting.fileStoragePath = FileAdapterShare.currentFilePath + ""
             EggIO().writeFile(settP, jsonO.toJSONString())*/
            EggUtil.writeSetting(this, "file_storage_path", FileAdapterShare.currentFilePath + "/")
            AppSetting.fileStoragePath = FileAdapterShare.currentFilePath + "/"
            finish()
        }
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        when (keyCode) {
            KeyEvent.KEYCODE_BACK -> {
                if (FileAdapterShare.currentFilePath == Environment.getExternalStorageDirectory().absolutePath) {
                    finish()
                    return false
                }

                val file = File(FileAdapterShare.currentFilePath).parentFile

                val fileL = file?.listFiles(
                    FileFilter {
                        it.isDirectory
                    })
                fileL?.sort()
                filesListV.adapter = FilesList(this, fileL!!)
                FileAdapterShare.currentFilePath = file.absolutePath
                findViewById<TextView>(R.id.title_text).text =
                    FileAdapterShare.currentFilePath
                return true
            }
        }
        return super.onKeyDown(keyCode, event)
    }
}
